package com.university.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.university.mapper.FoodMapper;
import com.university.pojo.cqe.command.CmdSaveFood;
import com.university.pojo.cqe.command.CmdUpdateFood;
import com.university.pojo.cqe.query.FoodQuery;
import com.university.pojo.dto.FoodUserDTO;
import com.university.pojo.entity.Food;
import com.university.service.FoodService;
import com.university.utility.ObjectUtils;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Date: 2021/5/11 17:17
 * @Description:
 */
@Service
public class FoodServiceImpl implements FoodService {

    @Resource
    private FoodMapper foodMapper;

    @Override
    public void saveFood(CmdSaveFood cmdSaveFood) {
        Food food = ObjectUtils.convert(cmdSaveFood, Food.class);
        foodMapper.insert(food);
    }

    @Override
    @SneakyThrows
    public void updateFood(CmdUpdateFood cmdUpdateFood) {
        Food food = ObjectUtils.convert(cmdUpdateFood, Food.class);
        int rows = foodMapper.updateById(food);
        if (rows == 0) {
            throw new Exception("id is not exist");
        }
    }

    @Override
    @SneakyThrows
    public void deleteFood(String id) {
        int rows = foodMapper.deleteById(id);
        if (rows == 0) {
            throw new Exception("id is not exist");
        }
    }

    @Override
    public IPage<Food> adminGetFoodPage(FoodQuery query) {
        IPage<Food> page = new Page<>(query.getPage(), query.getPageSize());
        return foodMapper.adminGetFoodPage(page, query);
    }

    @Override
    public List<FoodUserDTO> userGetFood(FoodQuery query) {
        return foodMapper.userGetFood(query);
    }

}
